//
//  MoreViewController.m
//  OrderSeat
//
//  Created by wangwei on 13-10-4.
//  Copyright (c) 2013年 wangwei. All rights reserved.
//

#import "MoreViewController.h"
#import "MoreCell.h"
#import "AboutViewController.h"
#import "ComplaintsViewController.h"
#import "FeedbackViewController.h"
#import "PlatformViewController.h"

@implementation MoreViewController

- (id)init
{
    self = [super initWithNibClass:[MoreViewController class]];
    if (self) {
        _dataSource = [[MoreDataSource alloc] initWithVC:self];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_dataSource initData];
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    if (self.contectTV.delegate) {
//        [self.contectTV reloadData];
//    }
    
    [_dataSource.rowList[0] setObject:[AppSetting cityName] forKey:@"title"];
    [self.contectTV reloadData];
}

- (void)setupUI
{
    [self createNavigation:@"更多"];
    self.contectTV.delegate = self.dataSource;
    self.contectTV.dataSource = self.dataSource;
}

- (void)selectedDidCity:(NSDictionary *)city
{
    NSMutableDictionary *row = _dataSource.rowList[0];
    //NSString *cityId = [city objectForKey:@"id"];
    NSString *cityName = [city objectForKey:@"name"];
    [row setObject:cityName forKey:@"title"];
        
    [self.contectTV reloadData];
}

@end

//==============================================================================
#pragma=========数据源实现========================================================
//==============================================================================

@implementation MoreDataSource

- (id)initWithVC:(id)viewController
{
    self = [super init];
    if(self) {
        self.viewController = viewController;
        self.rowList = [NSMutableArray arrayWithCapacity:10];
    }
    return self;
}

- (void)initData
{
    NSMutableDictionary *dic0 =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:[AppSetting cityName], @"title", nil];
    
    NSDictionary *dic1 =
    [NSDictionary dictionaryWithObjectsAndKeys:@"推送设置",
     @"title",@"more_push_icon.png",@"imageName", nil];
    NSDictionary *dic2 =
    [NSDictionary dictionaryWithObjectsAndKeys:@"关于美滋美位",
     @"title",@"more_about_icon.png",@"imageName", nil];
    NSDictionary *dic3 =
    [NSDictionary dictionaryWithObjectsAndKeys:@"意见反馈",
     @"title",@"more_opinion_icon.png",@"imageName", nil];
//    NSDictionary *dic4 =
//    [NSDictionary dictionaryWithObjectsAndKeys:@"告诉我们",
//     @"title",@"more_friend_icon.png",@"imageName", nil];
    NSDictionary *dic5 =
    [NSDictionary dictionaryWithObjectsAndKeys:@"餐厅投诉",
     @"title",@"more_complaints_icon.png",@"imageName", nil];
    NSDictionary *dic6 =
    [NSDictionary dictionaryWithObjectsAndKeys:@"检查更新",
     @"title",@"more_updata_icon.png",@"imageName", nil];
//    NSDictionary *dic7 =
//    [NSDictionary dictionaryWithObjectsAndKeys:@"支持我们(给我们打分吧)",
//     @"title",@"more_support_icon.png",@"imageName", nil];
    NSDictionary *dic8 =
    [NSDictionary dictionaryWithObjectsAndKeys:@"微信公众平台",
     @"title",@"more_platform_icon.png",@"imageName", nil];
    
    [_rowList addObject:dic0];
    [_rowList addObject:dic1];
    [_rowList addObject:dic2];
    [_rowList addObject:dic3];
//    [_rowList addObject:dic4];
    [_rowList addObject:dic5];
    [_rowList addObject:dic6];
//    [_rowList addObject:dic7];
    [_rowList addObject:dic8];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 44;
    if (indexPath.row == 0) return 66;
    if (indexPath.row == 6) return height+10;
    return height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [self setCellIdentifier:indexPath.row];
   
    MoreCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MoreCell" owner:self options:nil];
        cell = [array objectAtIndex:[self setIndex:indexPath.row]];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    [cell setUpUI:indexPath.row];
    
    NSDictionary* dic = [self.rowList objectAtIndex:indexPath.row];
    [cell setContect:dic row:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self addSubView:indexPath.row];
}

- (NSString*)setCellIdentifier:(int)tag
{
    if (tag == 0) return @"MoreCell_0";
    if (tag == 6) return @"MoreCell_2";
    return @"MoreCell_1";
}

- (int)setIndex:(int)tag
{
    if (tag == 0) return 0;
    if (tag == 6) return 2;
    return 1;
}

- (void)addSubView:(int)tag
{
    switch (tag) {
        case 0: {
            CitysViewController *cityVC = [[CitysViewController alloc] init];
            cityVC.parentViewController = _viewController;
            cityVC.delegate = _viewController;
            [self.viewController pushViewController:cityVC];
            break;
        }
            
        case 2:{
            AboutViewController *moreAboutVC =
            [[AboutViewController alloc] init];
            [self.viewController pushViewController:moreAboutVC];
            break;
        }
            
        case 3: {
            FeedbackViewController * feedbackVC =
            [[FeedbackViewController alloc] init];
            [self.viewController pushViewController:feedbackVC];
            break;
        }
            
        case 4: {
            ComplaintsViewController *complaintsVC =
            [[ComplaintsViewController alloc] init];
            [self.viewController pushViewController:complaintsVC];
            break;
        }
            
        case 5: {
            [self onCheckVersion];
            break;
        }
            
        case 6: {
            PlatformViewController *platformVC =
            [[PlatformViewController alloc] init];
            [self.viewController pushViewController:platformVC];
            break;
        }
    }
}

-(void)onCheckVersion
{
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];
    
    NSString *URL = @"http://itunes.apple.com/lookup?id=797520950";
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:URL]];
    [request setHTTPMethod:@"POST"];
    NSHTTPURLResponse *urlResponse = nil;
    NSError *error = nil;
    NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
    
    NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length] encoding:NSUTF8StringEncoding];
    NSDictionary *dic = [results JSONValue];
    NSArray *infoArray = [dic objectForKey:@"results"];
    
    if ([infoArray count]) {
        NSDictionary *releaseInfo = [infoArray objectAtIndex:0];
        NSString *lastVersion = [releaseInfo objectForKey:@"version"];
        
        if (![lastVersion isEqualToString:currentVersion]) {
            //trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新，是否前往更新？" delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil];
            alert.tag = 10000;
            [alert show];
        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本为最新版本" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            alert.tag = 10001;
            [alert show];
        }
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==10000) {
        if (buttonIndex==1) {
            NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com/cn/app/mei-zi-mei-wei/id797520950?mt=8"];
            [[UIApplication sharedApplication]openURL:url];
        }
    }
}

@end

